package cn.ococci.dm.Main.Tools;

import android.content.Context;
import android.content.SharedPreferences;

import cn.ococci.dm.Main.Const;


/**
 * Created by cyan on 15/9/21.
 *
 * 保存所有的sharePreferences 信息,并且建议key 可保存在当前的类中。
 *  key直接定义在当前的界面
 */
public class SpTool {

    public static SharedPreferences config;
    public static SharedPreferences.Editor editor;

    public static void init(Context c){
        config = c.getSharedPreferences(Const.SP_CONFIG,Context.MODE_PRIVATE);
    }

    public static SharedPreferences.Editor getEditor(){
        if(editor == null){
            editor = config.edit();
        }
        return editor;
    }

    /**
     * 清除所有键，但保留用户名，注销时使用
     */
    public static void clearAllData() {
        String username = getString(Const.LoginParams.USER_ACCOUNT, "");
        SharedPreferences.Editor editor = getEditor();
        editor.clear();
        editor.putString(Const.LoginParams.USER_ACCOUNT,username);
        editor.commit();
    }


   public static void putString(String key,String value){
       getEditor().putString(key, value).commit();
   }

   public static void putInt(String key,int value){
       getEditor().putInt(key, value).commit();
   }

   public static void putBoolean(String key,boolean value){
       getEditor().putBoolean(key, value).commit();
   }

    public  static void putFloat(String key,float value){
        getEditor().putFloat(key, value).commit();
    }

    public static void putLong(String key,long value){
        getEditor().putLong(key, value).commit();

    }


    public static String getString(String key,String defaultValue){
        return config.getString(key,defaultValue);
    }

    public static int getInt(String key,int defaultValue){
        return config.getInt(key, defaultValue);
    }

    public static long getLong(String key,int defaultValue){
        return config.getLong(key, defaultValue);
    }

    public static boolean getBoolean(String  key,boolean flag){
        return config.getBoolean(key,flag);
    }


    /**
     * 返回用户是否已经登录
     *
     * @return
     */
    public static boolean isLogin() {
        return config.getBoolean(Const.LoginParams.SP_KEY_LOGIN_FLAG, false);
    }

    /**

     * @param loginFlag
     */
    public static void setLoginFlag(boolean loginFlag) {
        getEditor().putBoolean(Const.LoginParams.SP_KEY_LOGIN_FLAG, loginFlag).commit();
    }





}
